#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <algorithm>
#include <string>
#include <iostream>

struct Patient {
    std::string id;
    int age;
    int order;
} patients[110];


bool MyCompare(const Patient& p1, const Patient& p2) {
    if (p1.age >= 60 && p2.age < 60) {
        return true;
    }
    if (p1.age < 60 && p2.age >= 60) {
        return false;
    }

    if (p1.age >= 60 && p2.age >= 60) {
        if (p1.age != p2.age) {
            return p1.age >= p2.age;
        }
        return p1.order < p2.order;
    }


    return p1.order < p2.order;

}

/**
 * 时间复杂度：O(nlogn)
 * 空间复杂度：O(n)
 * 
 * 病人排队
 * @return
 */
int main() {

    int n;
    scanf("%d", &n);

    for (int i = 0; i < n; i++) {
        std::cin >> patients[i].id >> patients[i].age;
        patients[i].order = i;
    }

    std::sort(patients, patients + n, MyCompare);

    for (int i=0; i < n; i++) {
        std::cout << patients[i].id << "\n";
    }


    return 0;
}



